using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Build.Utilities;
using System.Management;
using System.Diagnostics;
using Microsoft.Build.Framework;

namespace BizTalk.BuildGenerator.Tasks
{
    /// <summary>
    /// Task to create hosts
    /// </summary>    
    public class CreateHost : Task
    {
        bool _Trusted = true;
        bool _HostTracking = false;
        bool _IsDefault = false;        
        string _HostName;                
        string _HostType;
        string _GroupName;

        #region Properties
        /// <summary>
        /// Host Type
        /// </summary>
        [Required]
        public string HostType
        {
            get { return _HostType; }
            set { _HostType = value; }
        }
        /// <summary>
        /// GroupName
        /// </summary>
        [Required]
        public string GroupName
        {
            get { return _GroupName; }
            set { _GroupName = value; }
        }
        /// <summary>
        /// Trusted
        /// </summary>
        public bool Trusted
        {
            get { return _Trusted; }
            set { _Trusted = value; }
        }
        /// <summary>
        /// Host Tracking
        /// </summary>
        public bool HostTracking
        {
            get { return _HostTracking; }
            set { _HostTracking = value; }
        }
        /// <summary>
        /// IsDefault
        /// </summary>
        public bool IsDefault
        {
            get { return _IsDefault; }
            set { _IsDefault = value; }
        }
        /// <summary>
        /// HostName
        /// </summary>
        [Required]
        public string HostName
        {
            get { return _HostName; }
            set { _HostName = value; }
        }
        #endregion

        /// <summary>
        /// Execute method
        /// </summary>
        /// <returns></returns>
        public override bool Execute()
        {
            HostType hostType = (HostType)Enum.Parse(typeof(HostType), this._HostType);

            try
            {
                PutOptions options = new PutOptions();
                options.Type = PutType.CreateOnly;

                ManagementObject btsHostSetting = HostsHelper.GetHostSettingClass().CreateInstance();
                btsHostSetting["Name"] = this._HostName;
                btsHostSetting["HostType"] = (int)hostType;
                btsHostSetting["NTGroupName"] = this._GroupName;
                btsHostSetting["AuthTrusted"] = this._Trusted;
                if (hostType == BizTalk.BuildGenerator.Tasks.HostType.InProcess)
                {
                    btsHostSetting.SetPropertyValue("HostTracking", this._HostTracking);
                    btsHostSetting.SetPropertyValue("IsDefault", this._IsDefault);
                }
                else
                {
                    if (this._IsDefault || this._HostTracking)
                        Trace.WriteLine("Host Type is inprocess so Host Tracking and Is Default will be defaulted to false");
                }
                btsHostSetting.Put(options);                
            }
            catch (Exception ex)
            {
                Trace.WriteLine("CreateHost - " + this._HostName + " - failed: " + ex.Message);
                throw;
            }
            return true;
        }


    }
    /// <summary>
    /// Host types
    /// </summary>
    public enum HostType
    {
        /// <summary>
        /// Invalid host type
        /// </summary>
        Invalid,
        /// <summary>
        /// Inprocess host
        /// </summary>
        InProcess,
        /// <summary>
        /// Isolated host lie for example iis
        /// </summary>
        Isolated
    }
}
